package com.cj.cms.web.security;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.cj.cms.cache.ChannelArticleCache;
import com.cj.cms.entity.WebSite;
import com.cj.cms.service.WebSiteManager;
import com.cj.common.orm.hibernate.EntityManager;
import com.cj.common.web.struts2.CRUDActionSupport;
import com.cj.common.web.struts2.Struts2Utils;

public class WebSiteAction extends CRUDActionSupport<WebSite>{
	@Autowired
	private WebSiteManager webSiteManager;

	@Override
	public String list() {
		return mamagerSite();
	}
	public String mamagerSite(){
		try {
			return INPUT;
		} catch (Exception e) {
			setErrMessage(e.getMessage(), e);
			return ERROR;
		}
	}
	public void prepareMamagerSite() throws Exception{
		prepareModel();
	}
	@Override
	public EntityManager getEntityManager() {
		return webSiteManager;
	}
	
	/**
	 * @return 清理缓存
	 */
	public String clearCache(){
		try {
			ChannelArticleCache.getInstance().clearArticlePage();
			Struts2Utils.renderText("true");
		} catch (Exception e) {
			setErrMessage(e.getMessage());
			Struts2Utils.renderText("error:" + getErrMessage());
		}
		return null;
	}
	protected void prepareModel() throws Exception{
		if(id != null)
			entity = webSiteManager.get(id);
		else{
			List<WebSite> webSites = webSiteManager.getAll();
			if(webSites.isEmpty())
				entity = new WebSite();
			else
				entity = webSites.get(0);
		}
	}
	
}
